package GUI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class HorizontalScrollBar extends Component{

	public float size;

	public float valStart;
	public float valEnd;

	public boolean draggingStart;
	public boolean draggingEnd;
	public boolean draggingAll;

	public Component selectionStart;
	public Component selectionEnd;

	public int dragX;

	public float indexStart;
	public float indexEnd;

	public ArrayList<GregorianCalendar> dates;

	public boolean modeHours=false;
	public int total;

	public HorizontalScrollBar( ArrayList<GregorianCalendar> d,boolean modeHours)
	{
		this.modeHours=modeHours;
		dates = d;
		selectionStart = new Component();
		selectionStart.backColor = GV.processing.color(200, 200, 255, 100);

		selectionEnd = new Component();
		selectionEnd.backColor = GV.processing.color(200, 200, 255, 100);


		if(modeHours){	total = GV.totalHours;valEnd = (float)0.94;}
		else {total = GV.totalMonths; 	valEnd =  GV.processing.map((GV.dateEnd.get(Calendar.YEAR)-2001)*12+GV.dateEnd.get(Calendar.MONTH)-1,  1, GV.totalMonths,0,1);
/*valEnd = (float)0.98;*/}
	}

	public void drawBackLayer(){

		GV.processing.noStroke();
		GV.processing.fill(GV.LineColor);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.rect(x, y, width-3, height);
		GV.processing.noFill();

		for(int i=0;i<total-1;i++){
			int k =  (int) (GV.processing.map(i, 0, total-1, 0, this.width)); 
			GV.processing.stroke(255);
			GV.processing.strokeWeight(1);
			if(i%12==0){
				GV.processing.line(x+k, y-10, x+k, y+height);

			}
			else GV.processing.line(x+k, y, x+k, y+height);
		}
		
		GV.processing.noStroke();
		GV.processing.fill(GV.backGroundColor);
		GV.processing.rect(x, y+20, 130, 27);
		GV.processing.rect(x+width-130 , y+20, 130, 27);
		GV.processing.fill(GV.textColor);
		GV.processing.textFont(GV.plotFont,14);
		GV.processing.textAlign(GV.processing.LEFT);
		if(modeHours){
			GV.processing.text(GV.hourStart.get(Calendar.HOUR_OF_DAY)+" "+((GV.hourStart.get(Calendar.AM_PM)==0)?"AM":"PM"), x+5, y + 40);
		}
		else if(!GV.lp.ppb.playT)GV.processing.text(GV.monthName[GV.dateStart.get(Calendar.MONTH)]+" "+GV.dateStart.get(Calendar.YEAR), x+5, y + 40);
		GV.processing.textAlign(GV.processing.RIGHT);
		if(modeHours){
			GV.processing.text(GV.hourEnd.get(Calendar.HOUR)+" "+((GV.hourEnd.get(Calendar.AM_PM)==0)?"AM":"PM"), x+width -10, y + 40);
		}
		else if(!GV.lp.ppb.playT)GV.processing.text(GV.monthName[GV.dateEnd.get(Calendar.MONTH)]+" "+GV.dateEnd.get(Calendar.YEAR), x + width-10, y + 40);
		
	}

	public void draw()
	{
		int bh = (int)(size * width);
		if(bh < 5) bh = 5;

		GV.processing.noStroke();
		GV.processing.fill(GV.LineColor);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.rect(x, y, width-3, height);
		GV.processing.noFill();

		for(int i=0;i<total-1;i++){
			int k =  (int) (GV.processing.map(i, 0, total-1, 0, this.width)); 
			GV.processing.stroke(255);
			GV.processing.strokeWeight(1);
			if(i%12==0){
				GV.processing.line(x+k, y-10, x+k, y+height);

			}
			else GV.processing.line(x+k, y, x+k, y+height);
		}


		// Draw selectionStart region
		selectionStart.y = y;
		selectionStart.x = x + (int)(valStart * width);
		selectionStart.width = bh;
		selectionStart.height = height;
		//selectionStart.draw();

		// Draw selectionEnd region
		selectionEnd.y = y;
		selectionEnd.x = x + (int)(valEnd * width);
		selectionEnd.width = bh;
		selectionEnd.height = height;
		//selectionEnd.draw();     

		GV.gu.drawVGradient(selectionEnd.x + selectionEnd.width, selectionEnd.y,
				-(selectionEnd.x - selectionStart.x+ selectionEnd.width), selectionStart.height, 
				GV.processing.color(140, 140, 140), 255, GV.processing.color(140, 140, 140), 255, (float) 0.5);

		// Handle moving all the scrollbar
		if(draggingAll )
		{
			float d = (float)(GV.processing.mouseX - dragX) / width;
			float tmpV = valStart + d;
			float tmpV2 = valEnd + d;
			if(tmpV >= 0 && tmpV + size <= 1 && tmpV2 >= 0 && tmpV2 + size <= 1)
			{
				valStart = tmpV;
				valEnd = tmpV2;
			}
			dragX = GV.processing.mouseX;
		}

		// Handle start widget dragging.
		else if(draggingStart && GV.processing.mouseX < selectionEnd.x)
		{
			float d = (float)(GV.processing.mouseX - dragX) / width;
			float tmpV = valStart + d;
			if(tmpV >= 0 && tmpV + size <= 1 ) //&& (valEnd-tmpV)<(float)(0.5)
			{
				valStart = tmpV;
			}
			dragX = GV.processing.mouseX;
		}
		// Handle end widget dragging.
		else if(draggingEnd  && GV.processing.mouseX > (selectionStart.x + selectionStart.width))
		{
			float d = (float)(GV.processing.mouseX - dragX) / width;
			float tmpV = valEnd + d;
			if(tmpV >= 0 && tmpV + size <= 1)  //valEnd-tmpV<(float)(0.5)
			{
				valEnd = tmpV;
			}
			dragX = GV.processing.mouseX;
		}
		//else if(modeHours) System.out.println(draggingEnd+" " +(GV.processing.mouseX > (selectionStart.x + selectionStart.width)));

		indexStart = GV.processing.map(valStart, 0, 1, 0, total-1);
		if(modeHours){
			GV.hourStart = new GregorianCalendar(2001,0,1,(int)indexStart,0);
			GV.updateUfoToDisplay();
		}
		else{
			GV.dateStart = GV.getDateFromMonthNumber((int)indexStart);
			GV.dateStartIndex=(int)indexStart;
			GV.updateUfoToDisplay();
		}

		indexEnd = GV.processing.map(valEnd, (float)-0.02, (float)0.98,0, total - 1);
		if (indexEnd > total - 1) 
			indexEnd = total - 1;
		if(modeHours){
			GV.hourEnd = new GregorianCalendar(2001,0,1,(int)indexEnd,0);
			GV.updateUfoToDisplay();
		}
		else{
			GV.dateEnd = GV.getDateFromMonthNumber((int)indexEnd);
			GV.dateEndIndex = (int)indexEnd;
			GV.updateUfoToDisplay();
		}

		GV.processing.fill(GV.backGroundColor);
		GV.processing.rect(x, y+20, 130, 27);
		GV.processing.rect(x+width-130 , y+20, 130, 27);
		GV.processing.fill(GV.textColor);
		GV.processing.textFont(GV.plotFont,14);
		GV.processing.textAlign(GV.processing.LEFT);
		if(modeHours){
			GV.processing.text(GV.hourStart.get(Calendar.HOUR_OF_DAY)+" "+((GV.hourStart.get(Calendar.AM_PM)==0)?"AM":"PM"), x+5, y + 40);
		}
		else GV.processing.text(GV.monthName[GV.dateStart.get(Calendar.MONTH)]+" "+GV.dateStart.get(Calendar.YEAR), x+5, y + 40);
		GV.processing.textAlign(GV.processing.RIGHT);
		if(modeHours){
			GV.processing.text(GV.hourEnd.get(Calendar.HOUR)+" "+((GV.hourEnd.get(Calendar.AM_PM)==0)?"AM":"PM"), x+width -10, y + 40);
		}
		else GV.processing.text(GV.monthName[GV.dateEnd.get(Calendar.MONTH)]+" "+GV.dateEnd.get(Calendar.YEAR), x + width-10, y + 40);



	}

	public void mousePressed()
	{
		if (selectionStart.mouseOver() ) {
			int bh = (int)(size * width);
			if(bh < 10) bh = 10;
			int x1 = x + (int)(valStart * width);
			int x2 = x1 + bh;
			if(GV.processing.mouseX >= x1 && GV.processing.mouseX <= x2)
			{
				draggingStart = true;
				dragX = GV.processing.mouseX;
				return;
			}
		}
		else if (selectionEnd.mouseOver()) {
			int bh = (int)(size * width);
			if(bh < 10) bh = 10;
			int x1 = x + (int)(valEnd * width);
			int x2 = x1 + bh;
			//			if(GV.processing.mouseX >= x1 && GV.processing.mouseX <= x2)
			//			{
			draggingEnd = true;
			dragX = GV.processing.mouseX;
			return;
			//}
		}
		else if( GV.processing.mouseX > selectionStart.x + selectionStart.width && GV.processing.mouseX < selectionEnd.x
				&& GV.processing.mouseY > selectionStart.y && GV.processing.mouseY < selectionStart.y + selectionStart.height) {

			int bh = (int)(size * width);
			if(bh < 10) bh = 10;
			draggingAll = true;
			dragX = GV.processing.mouseX;
			return;

		}


	}

	public void mouseReleased()
	{
		draggingStart = false;
		draggingEnd = false;
		draggingAll = false;   
	}

}
